package com.c503.sc.myproject.worm.url;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName HtmlParserTest
 * @Description 学习jsoup对象
 * Author lenovo
 * Date 2018/12/8 18:05
 * Version 1.0
 **/
public class HtmlParserTest {

    //类型
    private static String threadType = "normalthread";

    public static void parse(String filePath) {

        File file = new File(filePath);

        try{
             Document document = Jsoup.parse(file,"gbk");

            Element element = document.getElementById("wrapper");

            if(element == null){
                System.out.println("id设置错误");
                return;
            }
            Elements contextElements = element.getElementsByClass("mainbox threadlist");

            Element tableElements = contextElements.get(0).getElementsByTag("table").last();

            Elements elements = tableElements.getElementsByTag("tbody");

            for(Element element1 : elements){
                String elementId = element1.id();
                if(elementId != null && elementId.contains(threadType)){
                    Elements authElement = element1.getElementsByClass("author");

                    Elements hotElement = element1.getElementsByClass("hot");
                    Elements newElement = element1.getElementsByClass("new");
                    Elements commonElement = element1.getElementsByClass("common");
                    Elements lockElement = element1.getElementsByClass("lock");

                    Elements numElement = element1.getElementsByClass("nums");
                    Elements lastPostElement = element1.getElementsByClass("lastpost");

                    Elements elementsTmp = new Elements();

                    if(hotElement != null && hotElement.size() > 0){
                        elementsTmp = hotElement;
                    }
                    else if(newElement != null && newElement.size() > 0){
                        elementsTmp = newElement;
                    }
                    else if(lockElement != null && lockElement.size() > 0){
                        elementsTmp = lockElement;
                    }
                    else{
                        System.out.println("找不到类型");
                        for(String className : element1.classNames()){
                            System.out.println("类型： " + className);
                        }
                    }

                    System.out.println("<------------------------>");
                    if(elementsTmp.size() > 0){
                        List<String> context = getName(elementsTmp);

                        System.out.println("小说名称： " + context.get(0));
                        System.out.println("id: " + context.get(1));
                        System.out.println("小说URL地址：" + context.get(2));
                        System.out.println("类型: " + context.get(3));

                    }else{
                        System.out.println("找不到类型");
                    }

                    String authName = authElement.get(0).getElementsByTag("cite").get(0).text();
                    String createTime = authElement.get(0).getElementsByTag("em").get(0).text();
                    System.out.println("作者名称： " + authName);
                    System.out.println("时间： " + createTime);
                }


            }

        }
        catch (Exception e){
            e.printStackTrace();
        }




    }


    public static List<String> getName(Elements hotElement){

        String novelName = null;
        String novelId = null;
        String novelUrl = null;
        String type = "系统";

        List<String> resultList = new ArrayList<String>();

        Elements novelElement = hotElement.get(0).getElementsByTag("span");
        if(novelElement != null && novelElement.size() > 0){
            novelName = hotElement.get(0).getElementsByTag("span").get(0).text();
            novelId = hotElement.get(0).getElementsByTag("span").get(0).id();
            novelUrl = hotElement.get(0).getElementsByTag("span").select("a").get(0).attr("href");
        }

        Elements typeElement = hotElement.get(0).getElementsByTag("em");

        if(typeElement != null && typeElement.size() > 0){
            type = hotElement.get(0).getElementsByTag("em").get(0).text();
        }

        resultList.add(novelName);
        resultList.add(novelId);
        resultList.add(novelUrl);
        resultList.add(type);

        return resultList;

    }


    public static void main(String[] args) {

        for(int i = 1; i <= 34; i++){
            String index = String.valueOf(i);
            String filePath = "E://data//" + "page" + index +".txt";
            parse(filePath);
        }


    }

}
